package monitor

import (
    "os"
    "fmt"
    "time"
    "runtime"
    "sync"
)

var ostype = runtime.GOOS

type DataFile struct {
	mutex sync.Mutex
	mp3En bool
	logEn bool
	fpmp3 string
	fplog string
	fdmp3 *os.File
	fdlog *os.File
	lastDay int
}

func (lf *DataFile) Mp3Open(name string) string{
	if lf.mp3En == false {
		return "none"
	}
	var path string
	if ostype == "windows"{
   		path = fmt.Sprintf(".\\logs\\%s\\%04d%02d%02d\\", name, time.Now().Year(), int(time.Now().Month()), time.Now().Day()) 
	}else if ostype == "linux"{
	    path = fmt.Sprintf("./logs/%s/%04d%02d%02d/", name, time.Now().Year(), int(time.Now().Month()), time.Now().Day()) 
	}
	
	err := os.MkdirAll(path , 0644)
	os.Chmod(path, 0644)
	if err != nil {
		lf.WriteLogln("creat dir faild ", path)
		os.Exit(1)
	} 
	var filename string
	if ostype == "windows"{
   		filename = fmt.Sprintf("%02d%02d%02d.mp3", time.Now().Hour(), time.Now().Minute(), time.Now().Second())
	}else if ostype == "linux"{
	    filename = fmt.Sprintf("%02d%02d%02d.mp3", time.Now().Hour(), time.Now().Minute(), time.Now().Second())
	}

	lf.fpmp3 = path + filename
	os.Chmod(lf.fpmp3, 0644)
	lf.fdmp3, err = os.OpenFile(lf.fpmp3, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
	//fmt.Println(lf.fd)
	if err != nil {
		lf.WriteLogln("creat file failed", lf.fpmp3)
		os.Exit(1)
	}
	return filename
}

func (lf *DataFile) Mp3Write(p []byte) {
	if lf.mp3En == false  || lf.fdmp3 == nil {
		return
	}
    _, err := lf.fdmp3.Write(p)
    if err != nil {
    	//fmt.Println("write file failed", lf.fpmp3)
    	//os.Exit(1)
    }
}

func (lf *DataFile) Mp3Close() () {
	if lf.mp3En == false {
		return
	}
	lf.fdmp3.Close()
}



func (lf *DataFile) LogOpen(name string) { 
	if lf.logEn == false {
		return
	}
	lf.mutex.Lock()
	//currentday := time.Now().Year()*10000 + int(time.Now().Month()*100) + time.Now().Day()
	currentday := time.Now().Day()*10000 + int(time.Now().Hour()*100) + (time.Now().Minute()/1)
	fmt.Println(" init ", currentday)
	if lf.lastDay != currentday || lf.fdlog == nil {
		lf.fdlog.Close()
		var path string
		if ostype == "windows"{
	   		path = fmt.Sprintf(".\\logs\\%s\\%08d",name ,currentday)  
		}else if ostype == "linux"{
	   		path = fmt.Sprintf("./logs/%s/%08d",name, currentday) 
		}	
		err := os.MkdirAll(path , 0644)
		if err != nil {
			fmt.Println("creat dir faild", path)
			os.Exit(1)
		} 

		if ostype == "windows"{
	   		lf.fplog = path + "\\nstar.log" 
		}else if ostype == "linux"{
	   		lf.fplog = path + "/nstar.log" 
		}	
		
		lf.fdlog, err = os.OpenFile(lf.fplog, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
		if err != nil {
    		lf.WriteLogln("creat file failed", lf.fplog)
        	os.Exit(1)
    	}
    	os.Chmod(lf.fplog, 0644)
		lf.lastDay = currentday
	}
	lf.mutex.Unlock()
}

func (lf *DataFile) LogWrite(p []byte) {

	fmt.Printf(string(p))
	
	if lf.logEn == false || lf.fdlog == nil{
		return
	}
    _, err := lf.fdlog.Write(p)
    if err != nil {
    	os.Exit(1)
    }
    
    //lf.fdlog.Sync()
}

func (lf *DataFile) LogClose() () {
	if lf.logEn == false {
		return
	}
	lf.fdlog.Close()
	lf.fdlog= nil
}

func (lf *DataFile) LogInit(logen, mp3en bool) {
	lf.logEn= logen
	lf.mp3En= mp3en
}


func (lf *DataFile)WriteLogf(format string, a ...interface{}) {
	str := fmt.Sprintf("%02d:%02d:%02d ", time.Now().Hour(), time.Now().Minute(), time.Now().Second())
	str += fmt.Sprintf(format, a...)
	lf.mutex.Lock()
	lf.LogWrite([]byte(str))
	lf.mutex.Unlock()
}

func (lf *DataFile)WriteLogln(a ...interface{}) {
	str := fmt.Sprintf("%02d:%02d:%02d ", time.Now().Hour(), time.Now().Minute(), time.Now().Second())
	str += fmt.Sprintln(a...)
	lf.mutex.Lock()
	lf.LogWrite([]byte(str))
	lf.mutex.Unlock()
}



